package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.service.ISysDeptService;

import com.ruoyi.system.domain.vo.SjModelCountVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SjModel;
import com.ruoyi.system.service.ISjModelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 审计模型库Controller
 * 
 * @author zrr
 * @date 2024-11-04
 */
@RestController
@RequestMapping("/model/model")
public class SjModelController extends BaseController
{
    @Autowired
    private ISjModelService sjModelService;
    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询审计模型库列表
     */
    @PreAuthorize("@ss.hasPermi('model:model:list')")
    @GetMapping("/list")
    public TableDataInfo list(SjModel sjModel)
    {
        startPage();
        List<SjModel> list = sjModelService.selectSjModelList(sjModel);
        return getDataTable(list);
    }

    /**
     * 导出审计模型库列表
     */
    @PreAuthorize("@ss.hasPermi('model:model:export')")
    @Log(title = "审计模型库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SjModel sjModel)
    {
        List<SjModel> list = sjModelService.selectSjModelList(sjModel);
        ExcelUtil<SjModel> util = new ExcelUtil<SjModel>(SjModel.class);
        util.exportExcel(response, list, "审计模型库数据");
    }

    /**
     * 获取审计模型库详细信息
     */
    @PreAuthorize("@ss.hasPermi('model:model:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(sjModelService.selectSjModelById(id));
    }

    /**
     * 新增审计模型库
     */
    @PreAuthorize("@ss.hasPermi('model:model:add')")
    @Log(title = "审计模型库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SjModel sjModel)
    {
        if(!sjModel.getDepartmentCode().isEmpty()){
            SysDept sd = deptService.selectDeptById(Long.parseLong(sjModel.getDepartmentCode()));
            sjModel.setDepartment(sd.getDeptName());
        }
        return toAjax(sjModelService.insertSjModel(sjModel));
    }

    /**
     * 修改审计模型库
     */
    @PreAuthorize("@ss.hasPermi('model:model:edit')")
    @Log(title = "审计模型库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SjModel sjModel)
    {
        if(!sjModel.getDepartmentCode().isEmpty()){
            SysDept sd = deptService.selectDeptById(Long.parseLong(sjModel.getDepartmentCode()));
            sjModel.setDepartment(sd.getDeptName());
        }
        return toAjax(sjModelService.updateSjModel(sjModel));
    }

    /**
     * 删除审计模型库
     */
    @PreAuthorize("@ss.hasPermi('model:model:remove')")
    @Log(title = "审计模型库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(sjModelService.deleteSjModelByIds(ids));
    }


    /**
     * 查询审计模型库数量
     */
    @PreAuthorize("@ss.hasPermi('model:model:count')")
    @GetMapping("/modelCount")
    public TableDataInfo getModelCount()
    {
        startPage();
        List<SjModelCountVO> list = sjModelService.selectSjModelCountList();
        return getDataTable(list);
    }
}
